﻿using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraEditors;
using System.Collections.Generic;
using System.Drawing;
using AutoSelfApp.Views;
using System;
using AutoSelfApp.Entity;
using System.Windows.Forms;
using System.Linq;

namespace AutoSelfApp
{
    public partial class MainForm : XtraForm
    {
        private SampleDataSource dataSource;
        private Dictionary<SampleDataGroup, PageGroup> groupsItemDetailPage;

        public MainForm()
        {
            InitializeComponent();
            windowsUIView.AddTileWhenCreatingDocument = DevExpress.Utils.DefaultBoolean.False;
            dataSource = new SampleDataSource();
            groupsItemDetailPage = new Dictionary<SampleDataGroup, PageGroup>();
            if (Screen.AllScreens.Count() != 1)
            {
                TitleForm frm2 = new TitleForm();
                frm2.StartPosition = FormStartPosition.Manual;
                frm2.Left = Screen.AllScreens[1].Bounds.Width;
                frm2.Location = new Point(Screen.AllScreens[1].Bounds.Left, Screen.AllScreens[1].Bounds.Top);
                frm2.WindowState = FormWindowState.Maximized;
                frm2.Show();
            }
            CreateLayout();
        }

        private void CreateLayout()
        {
            foreach (SampleDataGroup group in dataSource.Data.Groups)
            {
                tileContainer.Buttons.Add(new DevExpress.XtraBars.Docking2010.WindowsUIButton(group.Title, null, -1, DevExpress.XtraBars.Docking2010.ImageLocation.AboveText, DevExpress.XtraBars.Docking2010.ButtonStyle.PushButton, null, true, -1, true, null, false, false, true, null, group, -1, false, false));
                PageGroup pageGroup = new PageGroup();
                pageGroup.Parent = tileContainer;
                pageGroup.Caption = group.Title;
                pageGroup.SelectionChanged += PageGroup_SelectionChanged;
                windowsUIView.ContentContainers.Add(pageGroup);
                foreach (SampleDataItem item in group.Items)
                {
                    //ItemDetailPage itemDetailPage = new ItemDetailPage(item);
                    XtraUserControl itemDetailPage = null;
                    if (item.Title == "物价查询" || item.Title == "药品查询")
                    {
                        var pageCtl = new PriceQueryPage(item);
                        item.pageCtrl = pageCtl;
                        itemDetailPage = pageCtl;
                        pageCtl.StartTime();
                    }
                    else
                    {
                        var pageCtl = new HospGuide();
                        item.pageCtrl = pageCtl;
                        itemDetailPage = pageCtl;
                        pageCtl.StartTime();
                    }
                    itemDetailPage.Dock = System.Windows.Forms.DockStyle.Fill;
                    BaseDocument document = windowsUIView.AddDocument(itemDetailPage);
                    document.Caption = item.Title;
                    pageGroup.Items.Add(document as Document);
                    CreateTile(document as Document, item).ActivationTarget = pageGroup;
                }
            }
            windowsUIView.ActivateContainer(tileContainer);
        }

        private void PageGroup_SelectionChanged(object sender, DocumentEventArgs e)
        {
        }

        private Tile CreateTile(Document document, SampleDataItem item)
        {
            Tile tile = new Tile();
            tile.Document = document;
            tile.Group = item.GroupName;
            tile.Tag = item;
            tile.Elements.Add(CreateTileItemElement(item.Subtitle, item.IconImage, item.ImageLocation, item.TextLocation, 20));
            tile.Appearances.Selected.BackColor = tile.Appearances.Hovered.BackColor = tile.Appearances.Normal.BackColor = item.BackColor;
            tile.Appearances.Selected.BorderColor = tile.Appearances.Hovered.BorderColor = tile.Appearances.Normal.BorderColor = item.BackColor;
            tile.Click += new TileClickEventHandler(tile_Click);
            windowsUIView.Tiles.Add(tile);
            tileContainer.Items.Add(tile);
            return tile;
        }

        private TileItemElement CreateTileItemElement(string text, Image image, Point imgLocation, Point txtLocation, int fontSize)
        {
            TileItemElement element = new TileItemElement();
            element.TextAlignment = TileItemContentAlignment.MiddleCenter;
            element.Text = text;
            element.Appearance.Normal.FontSizeDelta = fontSize;
            element.Image = image;
            element.ImageSize = new Size(38, 38);
            element.ImageAlignment = TileItemContentAlignment.MiddleLeft;
            element.ImageToTextIndent = 10;
            return element;
        }

        private void tile_Click(object sender, TileClickEventArgs e)
        {
            PageGroup page = ((e.Tile as Tile).ActivationTarget as PageGroup);
            if (page != null)
            {
                page.Parent = tileContainer;
                IPageCtrl pctl = (e.Tile.Tag as SampleDataItem).pageCtrl;
                page.SetSelected((e.Tile as Tile).Document);
                pctl.ClearScreen();
                StartTime();
            }
        }

        public void Home()
        {
            windowsUIView.ActivateContainer(tileContainer);
        }

        public void StartTime()
        {
            PriceConstants.time = 120;
            timer1.Enabled = true;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PriceConstants.time = PriceConstants.time - 1;
            if (PriceConstants.time == 0)
            {
                timer1.Stop();
                Home();
            }
        }
    }
}